package org.linlinjava.litemall.web.controller;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.web.annotation.LogAnno;
import org.linlinjava.litemall.web.annotation.LoginShop;
import org.linlinjava.litemall.web.dto.AdminCouponDTO;
import org.linlinjava.litemall.web.service.WebCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电子优惠券控制器
 *
 * @author yliyun
 * @description：优惠券
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/web/coupon")
public class WebCouponController {
    /**
     * 电子团购服务
     */
    @Autowired
    private WebCouponService webCouponService;

    /**
     * 使用
     *
     * @param dto dto
     * @return {@link Object}
     */
    @PostMapping("/use")
    @LogAnno
    public Object use(@RequestBody @Valid AdminCouponDTO dto){
        return ResponseUtil.ok(webCouponService.use(dto));
    }

    /**
     * 查询
     *
     * @param barCode  条形码
     * @param shopId   商店id
     * @return {@link Object}
     */
    @GetMapping("/query")
    @LogAnno
    public Object query(String barCode, @LoginShop Integer shopId,
                        @RequestParam(required = false) List<Integer> goodsIds){
        return webCouponService.queryByCode(barCode,shopId,goodsIds);
    }

    /**
     * 绑定
     *
     * @param orderId 订单id
     * @param isBind  是绑定
     * @param id      id
     * @return {@link Object}
     */
    @GetMapping("bind")
    @LogAnno
    public Object bind(@NotNull Integer id,Integer orderId,boolean isBind){
        return webCouponService.bind( id,orderId,isBind);
    }

    /**
     * 查询订单id
     *
     * @param orderId 订单id
     * @return {@link Object}
     */
    @GetMapping("queryByOrderId")
    @LogAnno
    public Object queryByOrderId(Integer orderId){
        return webCouponService.queryByOrderId(orderId);
    }
}
